home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / chpass / util.c < prev   
Encoding:
C/C++ Source or Header  |  1990-07-12  |  4.1 KB  |  159 lines

  1. /*
  2.  * Copyright (c) 1988 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Berkeley.  The name of the
  11.  * University may not be used to endorse or promote products derived
  12.  * from this software without specific prior written permission.
  13.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16.  */
  17.  
  18. #ifndef lint
  19. static char sccsid[] = "@(#)util.c    5.9 (Berkeley) 3/27/89";
  20. #endif /* not lint */
  21.  
  22. #include <sys/types.h>
  23. #include <sys/time.h>
  24. #include <pwd.h>
  25. #include <stdio.h>
  26. #include <chpass.h>
  27. #include <strings.h>
  28. #include <ctype.h>
  29. #include "pathnames.h"
  30.  
  31. static int dmsize[] =
  32.     { -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  33. static char *months[] =
  34.     { "January", "February", "March", "April", "May", "June",
  35.       "July", "August", "September", "October", "November",
  36.       "December", NULL };
  37. char *
  38. ttoa(tval)
  39.     time_t tval;
  40. {
  41.     struct tm *tp;
  42.     static char tbuf[50];
  43.  
  44.     if (tval) {
  45.         tp = localtime(&tval);
  46.         (void)sprintf(tbuf, "%s %d, 19%d", months[tp->tm_mon],
  47.             tp->tm_mday, tp->tm_year);
  48.     }
  49.     else
  50.         *tbuf = '\0';
  51.     return(tbuf);
  52.  
  53. atot(p, store)
  54.     char *p;
  55.     time_t *store;
  56. {
  57.     register char *t, **mp;
  58.     static struct tm *lt;
  59.     time_t tval, time();
  60.     int day, month, year;
  61.  
  62.     if (!*p) {
  63.         *store = 0;
  64.         return(0);
  65.     }
  66.     if (!lt) {
  67.         unsetenv("TZ");
  68.         (void)time(&tval);
  69.         lt = localtime(&tval);
  70.     }
  71.     if (!(t = strtok(p, " \t")))
  72.         goto bad;
  73.     for (mp = months;; ++mp) {
  74.         if (!*mp)
  75.             goto bad;
  76.         if (!strncasecmp(*mp, t, 3)) {
  77.             month = mp - months + 1;
  78.             break;
  79.         }
  80.     }
  81.     if (!(t = strtok((char *)NULL, " \t,")) || !isdigit(*t))
  82.         goto bad;
  83.     day = atoi(t);
  84.     if (!(t = strtok((char *)NULL, " \t,")) || !isdigit(*t))
  85.         goto bad;
  86.     year = atoi(t);
  87.     if (day < 1 || day > 31 || month < 1 || month > 12 || !year)
  88.         goto bad;
  89.  
  90. #define    TM_YEAR_BASE    1900
  91. #define    EPOCH_YEAR    1970
  92. #define    DAYSPERNYEAR    365
  93. #define    DAYSPERLYEAR    366
  94. #define    HOURSPERDAY    24
  95. #define    MINSPERHOUR    60
  96. #define    SECSPERMIN    60
  97. #define    isleap(y) (((y) % 4) == 0 && ((y) % 100) != 0 || ((y) % 400) == 0)
  98.  
  99.     if (year < 100)
  100.         year += TM_YEAR_BASE;
  101.     if (year <= EPOCH_YEAR)
  102. bad:        return(1);
  103.     tval = isleap(year) && month > 2;
  104.     for (--year; year >= EPOCH_YEAR; --year)
  105.         tval += isleap(year) ?
  106.             DAYSPERLYEAR : DAYSPERNYEAR;
  107.     while (--month)
  108.         tval += dmsize[month];
  109.     tval += day;
  110.     tval = tval * HOURSPERDAY * MINSPERHOUR * SECSPERMIN;
  111.     tval -= lt->tm_gmtoff;
  112.     *store = tval;
  113.     return(0);
  114. }
  115.  
  116. print(fp, pw)
  117.     FILE *fp;
  118.     struct passwd *pw;
  119. {
  120.     register char *p;
  121.     char *getusershell(), *ttoa();
  122.  
  123.     fprintf(fp, "#Changing user database information for %s.\n",
  124.         pw->pw_name);
  125.     if (!uid) {
  126.         fprintf(fp, "Login: %s\n", pw->pw_name);
  127.         fprintf(fp, "Password: %s\n", pw->pw_passwd);
  128.         fprintf(fp, "Uid [#]: %d\n", pw->pw_uid);
  129.         fprintf(fp, "Gid [# or name]: %d\n", pw->pw_gid);
  130.         fprintf(fp, "Change [month day year]: %s\n", ttoa(pw->pw_change));
  131.         fprintf(fp, "Expire [month day year]: %s\n", ttoa(pw->pw_expire));
  132.         fprintf(fp, "Class: %s\n", pw->pw_class);
  133.         fprintf(fp, "Home directory: %s\n", pw->pw_dir);
  134.         fprintf(fp, "Shell: %s\n",
  135.             *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
  136.     }
  137.     else {
  138.         /* only admin can change "restricted" shells */
  139.         setusershell();
  140.         for (;;)
  141.             if (!(p = getusershell()))
  142.                 break;
  143.             else if (!strcmp(pw->pw_shell, p)) {
  144.                 fprintf(fp, "Shell: %s\n", *pw->pw_shell ?
  145.                     pw->pw_shell : _PATH_BSHELL);
  146.                 break;
  147.             }
  148.     }
  149.     p = strsep(pw->pw_gecos, ",");
  150.     fprintf(fp, "Full Name: %s\n", p ? p : "");
  151.     p = strsep((char *)NULL, ",");
  152.     fprintf(fp, "Location: %s\n", p ? p : "");
  153.     p = strsep((char *)NULL, ",");
  154.     fprintf(fp, "Office Phone: %s\n", p ? p : "");
  155.     p = strsep((char *)NULL, ",");
  156.     fprintf(fp, "Home Phone: %s\n", p ? p : "");
  157. }
  158.